package main

import (
	"fmt"
	"net/http"
	_ "net/http/pprof"
	"sync/atomic"
	"time"
)

func main() {
	//test()
	//Int64ToTimeLocation(1546937040,"America/Los_Angeles")

	timePackTest()
}

/*func test(){
	fmt.Println(encodeHelper.DESEncrypt("123456","tigerAaa"))
}*/

func timePackTest() {
	s := time.Now()
	fmt.Println(s.Clock())
}

func test() {
	now := time.Now()
	nowDay := GetDayString(now)
	nowHourInt := now.Hour()

	previousTime := now.Add(-1 * time.Nanosecond)
	previousDay := GetDayString(previousTime)
	previousHourInt := previousTime.Hour()
	fmt.Println(now, previousTime)
	fmt.Println(now.Sub(previousTime) > 0)

	fmt.Println(nowDay, nowHourInt, "   P:", previousDay, previousHourInt)
	t := make(map[string]int, 0)
	t["2021-01-27"] = 1
	t["2021-01-27"] = 2
	t["2021-01-27"] = 3
	t["2021-01-27"] = 5
	t["2021-01-27"] = 10
	t["2021-01-27"] = 9
	t["2021-01-27"] = 11

	for k, v := range t {
		fmt.Println(k, v)
		if nowDay == k && nowHourInt == v {
			fmt.Println(k, v)
			fmt.Println("我要做事")
		}
		if previousDay == k && previousHourInt == v {
			fmt.Println(k, v)
			fmt.Println("我要做事")
		}
	}
}

func GetDayString(t time.Time) string {
	return t.Format("2006-01-02")
}

func atomicT(c *int32) {
	defer func() {
		*c = atomic.AddInt32(c, -1)
		fmt.Println(*c)
	}()
	var x string = "Hi"
	fmt.Println(x, " World")
	fmt.Printf("%s World\n", x)
	time.Sleep(time.Millisecond * 50)
	return
}

func atomicTrun() {
	var c int32 = 0
	go func() {
		fmt.Println(http.ListenAndServe("localhost:8080", nil))
	}()
	for i := 0; ; i++ {
		time.Sleep(time.Millisecond * 2)
		if c < 20 {
			c++
			go atomicT(&c)
		}
	}
}

func GetDay() {
	first := time.Date(2018, 2, 3, 22, 22, 22, 0, time.Local)
	second := time.Date(2018, 2, 4, 20, 2, 3, 0, time.Local)
	subD := second.Sub(first)
	fmt.Printf("%v ", subD)
	fmt.Println(subD.Hours() / 24)
}

func sinceTest() {

	start := time.Now()
	time.Sleep(time.Second * 5)
	x := time.Since(start)
	if x < time.Minute {
		fmt.Println("1: ", x)
	} else {
		fmt.Println("2:", x)
	}
}

func unixTime() {
	var timestap int64
	timestap = 1546937040
	date := time.Unix(timestap, 0)
	fmt.Println(date)
}

func TimeToInt64(t time.Time) int64 {
	return t.Unix()
}
func Int64ToTime(tt int64) time.Time {
	return time.Unix(tt, 0)
}

//"Asia/Shanghai"
func TimeToInt64Location(t time.Time, locat string) int64 {
	timeStr := t.Format("2006-01-02 15:04:05")
	loc, _ := time.LoadLocation(locat)                                 //设置时区
	tt, _ := time.ParseInLocation("2006-01-02 15:04:05", timeStr, loc) //2006-01-02 15:04:05是转换的格式如php的"Y-m-d H:i:s"
	return tt.Unix()
}
func Int64ToTimeLocation(tt int64, locat string) {
	loc, _ := time.LoadLocation(locat)
	time.Local = loc
	fmt.Println(time.Unix(tt, 0))
}
